home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / ResUtil Package / resutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-22  |  3.0 KB  |  128 lines  |  [TEXT/KAHL]

  1. /* resutils.c  22 Aug 94  Eric Kidd */
  2.  
  3. # include "resutil.h"
  4.  
  5. /* resutils.c
  6. ** resource manipulation library
  7. **
  8. ** Functions for making resource manipulation moderately civilized. Originally designed for
  9. ** File Typer and inspired by the new Inside Macintosh volumes.
  10. */
  11.  
  12. /* OSErr ClearResID( short rf, OSType rType, short rID )
  13. ** 
  14. ** Remove any resources with the given type and ID from the file "rf". If somebody has
  15. ** written in extra copies of a resource, messing up the structure, this routine should
  16. ** remove them all.
  17. */
  18.  
  19. OSErr ClearResID( short rf, ResType rType, short rID )
  20. {
  21.     short savedRF;
  22.     OSErr err;
  23.     Handle theRes;
  24.     OSErr returnVal;
  25.     
  26.     savedRF = CurResFile( );
  27.     UseResFile( rf );
  28.     
  29.     /* Read somewhere that I should make sure there is no res before installing one */
  30.     /* If the resource is duplicated, keep on deleting until the ID is clear        */
  31.     /* Sometimes multiple resources will have been written with the same ID. Yech.    */
  32.     
  33.     theRes = Get1Resource( rType, rID );
  34.     while ( ResError( ) == noErr && theRes != NULL )
  35.     {
  36.         RmveResource( theRes );
  37.         
  38.         if ( ( returnVal = ResError( ) ) != noErr )
  39.         {
  40.             UseResFile( savedRF );
  41.             return returnVal;
  42.         }
  43.             
  44.         DisposeHandle( theRes );
  45.         UpdateResFile( rf );
  46.         theRes = Get1Resource( rType, rID );
  47.     }
  48.     
  49.     if ( ResError( ) == resNotFound )
  50.         returnVal = noErr;
  51.     else
  52.         returnVal = ResError( );
  53.     
  54.     UseResFile( savedRF );
  55.     return returnVal;
  56. }
  57.  
  58. /* OSErr InstallResource( short rf, Handle theRes, ResType rType,
  59. **                            short rID, StringPtr name, short attr )
  60. ** 
  61. ** Installs the handle's data as a res in "rf" with the specified information. Releases the resource
  62. ** when done, if successful.
  63. */
  64.  
  65. OSErr InstallResource( short rf, Handle theRes, ResType rType, short rID, StringPtr name, short attr )
  66. {
  67.     OSErr err;
  68.     short savedRF;
  69.     
  70.     savedRF = CurResFile( );
  71.     UseResFile( rf );
  72.     
  73.     err = ClearResID( rf, rType, rID );
  74.     if ( err == noErr )
  75.     {
  76.         AddResource( theRes, rType, rID, name );
  77.         
  78.         if ( ResError( ) == noErr )
  79.             SetResAttrs( theRes, attr );
  80.         if ( ResError( ) == noErr )
  81.             ChangedResource( theRes );
  82.         if ( ResError( ) == noErr )
  83.             WriteResource( theRes );
  84.         if ( ResError( ) == noErr )
  85.             ReleaseResource( theRes );
  86.             
  87.         err = ResError( );
  88.     }
  89.     
  90.     UseResFile( savedRF );
  91.     return err;
  92. }
  93.  
  94. /* OSErr CopyResource( ResType rType, short rID, short src, short dest )
  95. **
  96. ** Apple Computer's standard CopyResource procedure in C with a few modifications for greater
  97. ** reliability and capabilities. Uses InstallResource.
  98. */
  99.  
  100. OSErr CopyResource( ResType rType, short rID, short src, short dest )
  101. {
  102.     short savedRF;
  103.     Handle theRes;
  104.     short attr;
  105.     Str255 name;
  106.     
  107.     short scratchID;
  108.     ResType scratchType;
  109.     OSErr returnVal;
  110.         
  111.     savedRF = CurResFile( );
  112.     UseResFile( src );
  113.     
  114.     theRes = Get1Resource( rType, rID );
  115.     if ( theRes == NULL )
  116.     {
  117.         UseResFile( savedRF );
  118.         return ResError( );
  119.     }
  120.     
  121.     GetResInfo( theRes, &scratchID, &scratchType, name );
  122.     attr = GetResAttrs( theRes );
  123.     DetachResource( theRes );
  124.     UseResFile( savedRF );
  125.  
  126.     return InstallResource( dest, theRes, rType, rID, name, attr );
  127. }
  128.